home *** CD-ROM | disk | FTP | other *** search
- // Comments beginning with -LR- are changes made to
- // accomodate new Universal Headers
-
- // -LR- Changed GestaltEqu.h to Gestalt.h
- #include <Gestalt.h>
- // -LR- Changed SysEqu.h to LowMem.h
- #include <LowMem.h>
- #include <Devices.h>
-
- #include <stdio.h>
-
- #define gestaltArbitorAttr 'arb '
- #define gestaltSerialArbitrationExists 0
- #define dRAMBased 0x0040
- #define dOpened 0x0020
-
- // There is a bug in InterfaceLib on PowerPC machines.
- // Certain low memory accessors were not included in
- // InterfaceLib. One of these accessors is LMGetUnitTableEntryCount
- // The following routine is meant as a work-around for this
- // problem. You should remove this routine when Apple
- // fixes the missing accessors.
- #if defined(powerc) || defined (__powerc)
- pascal short LMGetUnitTableEntryCount()
- {
- return (*(short*) 0x01D2);
- }
- #endif
-
- // This is a list of drivers used for test purposes in this code
- Str255 gDriverNames[] =
- {
- "\p.NoDrvr", // non-existant driver
- "\p.AOut", // serial port A output
- "\p.BIn", // serial port B input
- "\p.BOut", // serial port B output
- "\p.AppleCD", // CD-ROM output
- "\p.MPP", // AppleTalk
- "\p.ASYC00", // a hard disk driver
- "\p.AIn" // serial port A input
- };
-
-
- Boolean SerialArbitrationExists(void);
- Boolean DriverIsOpen(StringPtr driverName);
- Boolean CRMInstalled(void);
-
- // Test Gestalt to see if serial arbitration exists
- // on this machine
- Boolean SerialArbitrationExists(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltArbitorAttr, &response);
- if (err)
- return false;
- return ((response >> gestaltSerialArbitrationExists) & 1);
- }
-
-
- Boolean DriverIsOpen(StringPtr driverName)
- {
- Boolean canOpen = false;
- Boolean match = false;
- short index = 0;
- short count;
- DCtlHandle dceHandle;
- StringPtr namePtr;
- DCtlHandle *theUnitTable;
-
- // -LR- changed this to a low memory accessor function
- count = LMGetUnitTableEntryCount();
-
- // -LR- changed this to a low memory accessor function
- theUnitTable = (DCtlHandle*)LMGetUTableBase();
-
- while ( !match && (index < count)) {
- // get handle to a device control entry
- dceHandle = theUnitTable[index];
-
- if (dceHandle) {
- if (!( (**dceHandle).dCtlFlags & dRAMBased) )
- // RAM based drivers have a handle to their driver
- namePtr = (StringPtr)(**dceHandle).dCtlDriver+18;
- else
- // ROM based drivers have a pointer to their driver
- namePtr = (StringPtr) (*(DCtlPtr)dceHandle).dCtlDriver+18;
-
- // not case sensitive, diacritical marks count
- if (RelString(driverName, namePtr, false, true) == 0) {
- match = true;
- canOpen = ((**dceHandle).dCtlFlags & dOpened) ? true : false;
- }
- }
- index++;
- }
- return canOpen;
- }
-
- Boolean CRMInstalled(void)
- {
- long response;
- OSErr err;
-
- err = Gestalt(gestaltCRMAttr, &response);
- if (err)
- return false;
- return ((response >> gestaltCRMPresent) & 1);
- }
-
- #define TEST_CALL
- main()
- {
- OSErr err = noErr;
- SInt16 i;
- SInt16 numDrivers;
-
- printf("Test of serial port stuff\n");
- if (CRMInstalled())
- printf("Communication Resource Manager is installed.\n");
- if (SerialArbitrationExists())
- printf("Serial Arbitration exists\n");
-
- #ifdef TEST_CALL
- numDrivers = sizeof(gDriverNames) / sizeof(Str255);
- for (i = 0; i < numDrivers; i++)
- printf("DriverIsOpen(%#s)\treturned %s\n",
- gDriverNames[i], DriverIsOpen(gDriverNames[i])?"True ":"False");
- #endif
-
- #ifdef TEST_OPEN
- SInt16 refNum;
-
- if (!DriverIsOpen("\p.AIn"))
- err = OpenDriver("\p.AIn", &refNum);
- if (err)
- printf("OpenDriver returned %d\n", err);
- err = CloseDriver(refNum);
- if (err)
- printf("CloseDriver returned %d\n", err);
- #endif
- }